#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/io.h>

#define LED_MAJOR 200
#define LED_NAME "led"

#define PMU_GRF_BASE						      (0xFDC20000)
#define PMU_GRF_GPIO0C_IOMUX_L				(PMU_GRF_BASE + 0x0010)
#define PMU_GRF_GPIO0C_DS_0					  (PMU_GRF_BASE + 0X0090)
#define GPIO0_BASE						        (0xFDD60000)
#define GPIO0_SWPORT_DR_H				      (GPIO0_BASE + 0X0004)
#define GPIO0_SWPORT_DDR_H				    (GPIO0_BASE + 0X000C)

#define LEDOPEN 1
#define LEDCLOSE 0

/* 映射后的寄存器虚拟地址指针 */
static void __iomem *PMU_GRF_GPIO0C_IOMUX_L_PI;
static void __iomem *PMU_GRF_GPIO0C_DS_0_PI;
static void __iomem *GPIO0_SWPORT_DR_H_PI;
static void __iomem *GPIO0_SWPORT_DDR_H_PI;

// led gpio初始化操作
void gpio_init(void){
    u32 val = 0;
    // 设置GPIO0_c0为GPIO功能
    val = readl(PMU_GRF_GPIO0C_IOMUX_L_PI);
    val &= ~(0x7 << 0); //最低三位置0
    val |= ((0x7 << 16) | (0x0 << 0)); // 16 17 18位置1其他不变，bit2:0：0，用作GPIO0_C0
    writel(val, PMU_GRF_GPIO0C_IOMUX_L_PI);

    // 设置GPIO_C0驱动能力为level5
    val = readl(PMU_GRF_GPIO0C_DS_0_PI);
    val &= ~(0x3f << 0);  // 0 ~ 5置0
    val |= ((0x3f << 16) | (0x3f << 0)); // 16 ~ 21置1，0~5置1同时用作GPIO0c0
    writel(val, PMU_GRF_GPIO0C_DS_0_PI);

    // 设置GPIOO0_c0为输出
    val = readl(GPIO0_SWPORT_DDR_H_PI);
    val &= ~(0x1 << 0); // 0置0
    val |= ((0x1 << 16) | (0x1 << 0)); // 16置1,0置1
    writel(val, GPIO0_SWPORT_DDR_H_PI);

    // 设置GPIO_c0为低电平，关闭LED
    val = readl(GPIO0_SWPORT_DR_H_PI);
    val &= ~(0x1 << 0);
    val |= ((0x1 << 16) | (0x0 << 0));
    writel(val, GPIO0_SWPORT_DR_H_PI);
}


// 真实物理地址映射虚拟内存函数
void led_remap(void){

  PMU_GRF_GPIO0C_IOMUX_L_PI = ioremap(PMU_GRF_GPIO0C_IOMUX_L, 4);
	PMU_GRF_GPIO0C_DS_0_PI = ioremap(PMU_GRF_GPIO0C_DS_0, 4);
	GPIO0_SWPORT_DR_H_PI = ioremap(GPIO0_SWPORT_DR_H, 4);
	GPIO0_SWPORT_DDR_H_PI = ioremap(GPIO0_SWPORT_DDR_H, 4);

}

void led_releaseMap(void){
  // 取消地址映射
  iounmap(PMU_GRF_GPIO0C_IOMUX_L_PI);
  iounmap(PMU_GRF_GPIO0C_DS_0_PI);
  iounmap(GPIO0_SWPORT_DR_H_PI);
  iounmap(GPIO0_SWPORT_DDR_H_PI);
}


void led_switch(int status){
  u32 val = 0;

  if(status == LEDOPEN){
  // 开灯
  val = readl(GPIO0_SWPORT_DR_H_PI);
  val &= ~(0X1 << 0); /* bit0 清零*/
  val |= ((0X1 << 16) | (0X1 << 0));	/* bit16 置1，允许写bit0，
                         bit0，高电平*/ 
  writel(val, GPIO0_SWPORT_DR_H_PI);  

  }else if(status == LEDCLOSE){

  // 关灯
  val = readl(GPIO0_SWPORT_DR_H_PI);
  val &= ~(0X1 << 0); /* bit0 清零*/
  val |= ((0X1 << 16) | (0X0 << 0));	/* bit16 置1，允许写bit0，
                         bit0，低电平	*/
  writel(val, GPIO0_SWPORT_DR_H_PI); 

  }

}


static int led_open(struct inode* inode, struct file* filp){

  return 0;
}

static int led_release(struct inode* inode, struct file* filp){

  return 0;
}

static ssize_t led_write(struct file* filp, const char __user* buf, size_t count, loff_t* ppos){

  int ret;
  unsigned char databuf[1];
  unsigned char state;

  ret = copy_from_user(databuf, buf, count);
  if(ret < 0){
    printk("kernel write failed\r\n");
    return -EFAULT;
  }

  state = databuf[0];

  if(state == LEDOPEN){
    led_switch(LEDOPEN);
  }else if(state == LEDCLOSE){
    led_switch(LEDCLOSE);
  }

  return 0;
}

/* 字符设备操作集*/
static const struct file_operations led_fops = {
  .owner = THIS_MODULE,
  .write = led_write,
  .open = led_open,
  .release = led_release,
};

/*注册驱动加载卸载*/

static int __init led_init(void){ // 入口
  int ret = 0;
  led_remap();  // 内存映射
  gpio_init();  // 初始化led灯
  ret = register_chrdev(LED_MAJOR, LED_NAME, &led_fops);   // 注册字符设备
  if(ret < 0){
    printk("register chrdev failed!\r\n");
    return -EIO;
  }
  printk("led_init\r\n");
  return 0;
}

static void __exit led_exit(void){ // 出口

  led_releaseMap();  // 注销内存映射
  unregister_chrdev(LED_MAJOR, LED_NAME);  // 注销
  printk("led_exit\r\n");

}

module_init(led_init);
module_exit(led_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Narnat");